{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import joblib\n",
    "import datetime as dt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_01_plc = pd.read_csv('../raw_data/train_add/01/PLC/plc.csv')\n",
    "train_02_plc = pd.read_csv('../raw_data/train_add/02/PLC/plc.csv')\n",
    "train_03_plc = pd.read_csv('../raw_data/train_add/03/PLC/plc.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_time(plc_df):\n",
    "    plc_df['time'] = pd.to_datetime(plc_df.time,format='%H:%M:%S:%f')\n",
    "#     plc_df = plc_df.sort_values(by='time')\n",
    "    plc_df = plc_df.reset_index(drop=True)\n",
    "    return plc_df\n",
    "\n",
    "def CalRULR(offset,start,cur,total_life):\n",
    "    '''\n",
    "    描述：\n",
    "        offset表示第几段(从1开始计算),其余输入为TimeStamp，输出单位是min.数据是5分钟中的最后一分钟\n",
    "        \n",
    "    '''\n",
    "    cost = (offset*5-1) + (cur-start).value/(1e9*60) #min\n",
    "    rulr = 1 - cost/total_life\n",
    "    return rulr\n",
    "\n",
    "def CalCostLife(offset,start,cur,base_cost):\n",
    "    '''\n",
    "    描述：\n",
    "        计算已经花费的有效时间(CL)\n",
    "    '''\n",
    "    cost = (offset*5-1) + (cur-start).value/(1e9*60) #min\n",
    "    cost_life = base_cost + cost\n",
    "    return cost_life\n",
    "\n",
    "def CalCostLifeInterval(offset,base_cost):\n",
    "    '''\n",
    "    描述：\n",
    "        计算已经花费的有效时间区间(CLI, Cost Life Interval)\n",
    "    '''\n",
    "    cost_interval = offset*5\n",
    "    cost_life_interval = base_cost + cost_interval\n",
    "    return cost_life_interval\n",
    "\n",
    "def TransTime(data_plc, csv_nos=48, base_cost=0, total_time =240):\n",
    "    '''\n",
    "    描述：\n",
    "        把plc文件中的时间转化为剩余有用寿命比例RULR\n",
    "    参数：\n",
    "        data_plc：\n",
    "        csv_nos：plc对应的sensor文件数量\n",
    "        total_time:寿命\n",
    "    '''\n",
    "    data_plc = data_plc.loc[data_plc['csv_no']<=csv_nos]\n",
    "    rulr = []\n",
    "    cl = []\n",
    "    cli = []\n",
    "    for i in range(1,csv_nos+1):\n",
    "        time_seg = data_plc.loc[data_plc['csv_no']==i, 'time']\n",
    "        start = time_seg.min()\n",
    "        rulr.append(time_seg.apply(lambda x:CalRULR(i, start, x, total_time)).values)\n",
    "        cl.append(time_seg.apply(lambda x:CalCostLife(i, start, x, base_cost)).values)\n",
    "        cli.append(time_seg.apply(lambda x:CalCostLifeInterval(i, base_cost)).values)\n",
    "        \n",
    "    rulr_arr = np.hstack(rulr)\n",
    "    cl_arr = np.hstack(cl)\n",
    "    cli_arr = np.hstack(cli)\n",
    "    \n",
    "    result = data_plc.drop(columns=['time'])\n",
    "    result['RULR'] = rulr_arr\n",
    "    result['CL'] = cl_arr\n",
    "    result['CLI'] = cli_arr\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 原始数据被覆盖\n",
    "train_01_plc = process_time(train_01_plc)\n",
    "train_02_plc = process_time(train_02_plc)\n",
    "train_03_plc = process_time(train_03_plc)\n",
    "\n",
    "data_01 = TransTime(train_01_plc, 48, 0, 240)\n",
    "data_02 = TransTime(train_02_plc, 48, 0, 240)\n",
    "data_03 = TransTime(train_03_plc, 37, 0, 185)  # train_03_plc只有37份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./train_plc_RULR/train_03_plc.lz4']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if not os.path.exists('train_plc_RULR'):\n",
    "    os.mkdir('train_plc_RULR')\n",
    "output_dir = './train_plc_RULR'\n",
    "joblib.dump(data_01,os.path.join(output_dir,'train_01_plc.lz4'))\n",
    "joblib.dump(data_02,os.path.join(output_dir,'train_02_plc.lz4'))\n",
    "joblib.dump(data_03,os.path.join(output_dir,'train_03_plc.lz4'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
